package com.saver.modules.sys.controller;

import com.saver.common.db.JdbcTool;
import com.saver.common.utils.R;
import com.saver.modules.sys.dao.SysMenuDao;
import com.saver.modules.sys.entity.SysMenuEntity;
import com.saver.modules.sys.entity.SysUserEntity;
import com.saver.modules.sys.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * 相关下载
 *
 * @author yuxiaolong
 * @email 1005471232@qq.com
 * @date 2018年08月30日 下午11:22
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends AbstractController {

    @Autowired
    private SysMenuDao sysMenuDao;

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 菜单
     */
    @GetMapping("/nav")
    public R nav() {
        SysUserEntity user = getUser();
        Set<Long> roleIds = user.getRoleIds();
        return sysMenuService.nav(roleIds,getUserId());
    }


    /**
     * 所有菜单
     */
    @GetMapping("/all")
    public R all() {
        return sysMenuService.all();
    }

    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        return sysMenuService.queryPage(params);
    }

    @PostMapping("/add")
    public R add(@RequestBody SysMenuEntity sysMenuEntity) {
        sysMenuDao.save(sysMenuEntity);
        return R.ok();
    }

    @GetMapping("/info")
    public R info(@RequestParam("id") Integer id) {
        return sysMenuService.info(id);
    }

    @PostMapping("/update")
    public R update(@RequestBody SysMenuEntity sysMenuEntity) {
        sysMenuDao.save(sysMenuEntity);
        return R.ok();
    }

    @PostMapping("/delete")
    public R delete(@RequestBody Integer menuId) {
        return sysMenuService.deleteMenu(menuId);
    }
}
